package com.domor.xxx.system.service;

import com.domor.xxx.common.utils.TreeUtils;
import com.domor.xxx.system.domain.SysArea;
import com.domor.xxx.system.mapper.SysAreaMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <h3>地区 service</h3>
 * @author liyuyang
 * @date 2021/2/4
 */
@Service
public class SysAreaService {

    @Autowired
    private SysAreaMapper dao;

    @Cacheable("XXX:area:")
    public List<SysArea> getAll() throws Exception{
        List<SysArea> all = dao.getAll();
        List<SysArea> areas = TreeUtils.buildByRecursive(all, "Id", "ParentId", "Children");
        return areas;
    }

    /**
     * 获取下级地区列表
     * @param parentId
     * @return
     */
    public List<SysArea> getAreasByPid(int parentId) {
        return dao.getAreasByPid(parentId);
    }

    /**
     * 根据地区获取所在市
     * @param areaId
     * @return
     */
    public SysArea getAreaByAreaId(int areaId) {
        return dao.getAreaByCid(areaId);
    }

}
